package UILayer;

import CtrLayer.CustomerCtr;
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;
import javax.swing.border.*;

public class GroupManager extends JInternalFrame
{
	private JPanel contentPane;
	private JTextField terms_tf;
	private JTextField discount_tf;
	private JTextField name_tf;
	private String name;
	private JPanel panel;
	private CustomerCtr group_ctr;
	private JLabel[] error_lbls;
	private JLabel[] empty_error_lbls;
	private JLabel inc_disc_lbl;
	private JLabel inc_name_lbl;
	private String old_name;
	int view;

	/**
	 * Create the frame.
	 */
	public GroupManager(String i_name)
	{
		super("Add new Group", true, true, true, true);
		if (i_name != null)
		{
			this.setTitle("Edit group");
		}
		name = i_name;
		group_ctr = new CustomerCtr();

		setBounds(100, 100, 423, 231);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(new BorderLayout(0, 0));

		panel = new JPanel();
		contentPane.add(panel, BorderLayout.CENTER);
		panel.setLayout(null);

		JPanel panel_1 = new JPanel();
		contentPane.add(panel_1, BorderLayout.SOUTH);
		panel_1.setLayout(new BorderLayout(0, 0));

		JButton ok_btn = new JButton("Ok");

		ok_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				try_to_create_or_edit_product();
			}
		});

		panel_1.add(ok_btn, BorderLayout.WEST);

		JButton cancel_btn = new JButton("Cancel");
		cancel_btn.setHorizontalAlignment(SwingConstants.RIGHT);
		cancel_btn.setForeground(Color.BLACK);
		cancel_btn.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				dispose();
			}
		});
		cancel_btn.setBackground(new Color(240, 240, 240));
		panel_1.add(cancel_btn, BorderLayout.EAST);

		make_lbls();
		make_tfs();
		make_error_lbls();
		make_empty_error_lbls();
		make_logical_error_lbls();

	}

	private void make_logical_error_lbls()
	{
		inc_disc_lbl = new JLabel("<html>The discount must be<br/>a number between 0 and 100.</html>");
		inc_disc_lbl.setForeground(Color.RED);
		inc_disc_lbl.setLabelFor(name_tf);
		inc_disc_lbl.setBounds(215, 58, 230, 42);
		inc_disc_lbl.setVisible(false);
		panel.add(inc_disc_lbl);

		inc_name_lbl = new JLabel("The name is already taken.");
		inc_name_lbl.setForeground(Color.RED);
		inc_name_lbl.setBounds(202, 45, 290, 14);
		inc_name_lbl.setVisible(false);
		panel.add(inc_name_lbl);
	}

	private void make_lbls()
	{
		JLabel lblNewLabel = new JLabel("Group data");
		lblNewLabel.setFont(new Font("Times New Roman", Font.BOLD, 14));
		lblNewLabel.setBounds(10, 11, 138, 14);
		panel.add(lblNewLabel);

		JLabel name_lbl = new JLabel("Name");
		name_lbl.setBounds(10, 45, 67, 14);
		panel.add(name_lbl);

		JLabel terms_lbl = new JLabel("Terms of payment");
		terms_lbl.setBounds(10, 101, 93, 14);
		panel.add(terms_lbl);

		JLabel discount_lbl = new JLabel("Discount");
		discount_lbl.setBounds(10, 73, 79, 14);
		panel.add(discount_lbl);

		JLabel perc_lbl = new JLabel("%");
		perc_lbl.setBounds(202, 73, 86, 14);
		panel.add(perc_lbl);
	}

	private void make_tfs()
	{

		name_tf = new JTextField();
		name_tf.setBounds(113, 42, 86, 20);
		panel.add(name_tf);
		name_tf.setColumns(10);
		terms_tf = new JTextField();
		terms_tf.setBounds(113, 98, 86, 20);
		panel.add(terms_tf);
		terms_tf.setColumns(10);

		discount_tf = new JTextField();
		discount_tf.setBounds(113, 70, 86, 20);
		panel.add(discount_tf);
		discount_tf.setColumns(10);

		if (name != null)
		{
			fill_tfs();
		}
	}

	private void fill_tfs()
	{
		String[] data = group_ctr.give_string_group(name);
		old_name = data[0];
		name_tf.setText(data[0]);
		name_tf.setEditable(false);
		terms_tf.setText(data[1]);
		discount_tf.setText(data[2]);
	}

	private void try_to_create_or_edit_product()
	{
		if (check_for_correctness())
		{

			String new_name = name_tf.getText();
			String terms = terms_tf.getText();
			double disc = Double.parseDouble(discount_tf.getText());

			if (name == null)
			{
				if ((!group_ctr.is_group(new_name))
						|| (group_ctr.existing_groups()))
				{
					group_ctr.add_group(new_name, terms, disc);
					JOptionPane.showMessageDialog(this,
							"Successully created a group.", "Success",
							JOptionPane.INFORMATION_MESSAGE);
					this.dispose();
				}
				else
				{
					JOptionPane
							.showMessageDialog(
									this,
									"Couldn't create a group.\nGroup with this name already exists.",
									"Error", JOptionPane.INFORMATION_MESSAGE);
				}
			}
			else
			{
				if ((group_ctr.is_group(name)) || name.equals(old_name))
				{
					group_ctr.update_group(new_name, terms, disc);
					JOptionPane.showMessageDialog(this,
							"Successully edited a Group's information.",
							"Success", JOptionPane.INFORMATION_MESSAGE);
					this.dispose();
				}
				else
				{
					JOptionPane.showMessageDialog(this,
							"Couldn't edit a group.", "Error",
							JOptionPane.INFORMATION_MESSAGE);
				}
			}
		}
	}

	private boolean check_for_correctness()
	{
		inc_name_lbl.setVisible(false);
		inc_disc_lbl.setVisible(false);
		empty_error_lbls[0].setVisible(false);
		empty_error_lbls[1].setVisible(false);
		empty_error_lbls[2].setVisible(false);
		boolean[] display_lbl = { false, false };
		String discount = discount_tf.getText();
		if (group_ctr.is_group(name) && (name_tf.isEditable()))
		{
			display_lbl[0] = true;
		}
		try
		{
			double disc = Double.parseDouble(discount);
			if ((disc >= 0) && (disc <= 100))
			{
				display_lbl[1] = false;
			}
			else
			{
				display_lbl[1] = true;
			}
		}
		catch (Exception e)
		{
			display_lbl[1] = true;
		}

		return display_error_lbls(display_lbl);
	}

	private boolean display_error_lbls(boolean[] display_lbl)
	{
		boolean returner = true;
		if (name_tf.getText().equals(""))
		{
			empty_error_lbls[0].setVisible(true);
			returner = false;
		}
		else if (display_lbl[0])
		{
			inc_name_lbl.setVisible(true);
			returner = false;
		}
		if (terms_tf.getText().equals(""))
		{
			empty_error_lbls[1].setVisible(true);
			returner = false;
		}
		if (discount_tf.getText().equals(""))
		{
			empty_error_lbls[2].setVisible(true);
			returner = false;
		}
		else if (display_lbl[1])
		{
			inc_disc_lbl.setVisible(true);
			returner = false;
		}
		return returner;
	}

	private void make_error_lbls()
	{
		int[][] coords = { { 209, 160 }, { 209, 191 }, { 209, 222 },
				{ 209, 253 }, { 209, 284 }, { 516, 45 }, { 516, 73 },
				{ 516, 101 }, { 516, 129 } };
		error_lbls = new JLabel[9];
		for (int i = 0; i <= 8; i++)
		{
			error_lbls[i] = new JLabel("Insert number correctly.");
			error_lbls[i].setForeground(Color.RED);
			error_lbls[i].setBounds(coords[i][0], coords[i][1], 150, 14);
			error_lbls[i].setVisible(false);
			panel.add(error_lbls[i]);
		}
	}

	private void make_empty_error_lbls()
	{
		int[][] coords = { { 209, 45 }, { 209, 101 }, { 209, 73 } };
		empty_error_lbls = new JLabel[3];
		for (int i = 0; i <= 2; i++)
		{
			empty_error_lbls[i] = new JLabel("Must be filled.");
			empty_error_lbls[i].setForeground(Color.RED);
			empty_error_lbls[i].setBounds(coords[i][0], coords[i][1], 150, 14);
			empty_error_lbls[i].setVisible(false);
			panel.add(empty_error_lbls[i]);
		}

	}
}
